package indiwood.controller;

import indiwood.model.AwardVO;
import indiwood.model.CinemaVO;
import indiwood.model.DirectorVO;
import indiwood.model.MovieVO;
import indiwood.model.ScheduleDateVO;
import indiwood.model.ShowingVO;
import indiwood.model.service.AdminService;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller
@RequestMapping("/admin")
public class AdminController extends MultiActionController {
	@Resource(name = "adminServiceImpl")
	private AdminService adminService;
	@RequestMapping("/{viewId}_view.do")
	public String showView(@PathVariable("viewId") String viewId) {
		return viewId+"_view.admin";
	}
	@RequestMapping("/directorList.do")
	public ModelAndView directorList(String pageNo){
		return new ModelAndView("directorList.admin", "dlistvo", adminService.directorList(pageNo));
	}
	@RequestMapping("/director_content.do")
	public ModelAndView directorContent(int directorNo){
		return new ModelAndView("director_content.admin", "directorDetailMap", adminService.directorContent(directorNo));
	}
	@RequestMapping(value="/director_write.do", method=RequestMethod.POST)
	public String directorWrite(DirectorVO dvo, MultipartFile uploadFile){
		adminService.directorWrite(dvo, uploadFile);
		return "redirect:director_content.do?directorNo="+dvo.getDirectorNo();
	}
	@RequestMapping("/director_delete.do")
	public String directorDelete(int directorNo){
		adminService.directorDelete(directorNo);
		return "redirect:directorList.do";
	}
	@RequestMapping("/director_updateView.do")
	public ModelAndView directorUpdateView(int directorNo){
		return new ModelAndView("director_update.admin", "directorVO", adminService.directorUpdateView(directorNo));
	}
	@RequestMapping(value="/director_update.do", method=RequestMethod.POST)
	public String directorUpdate(DirectorVO dvo, MultipartFile uploadFile){
		adminService.directorUpdate(dvo, uploadFile);
		return "redirect:director_content.do?directorNo="+dvo.getDirectorNo();
	}
	@RequestMapping("/movieList.do")
	public ModelAndView movieList(String pageNo){
		return new ModelAndView("movieList.admin","mListVO", adminService.movieList(pageNo));
	}
	@RequestMapping("/findAllDirector.do")
	public ModelAndView findAllDirector(){
		return new ModelAndView("movie_write_view.admin","directorList",adminService.findAllDirector());
	}
	@RequestMapping(value="/movieWrite.do", method=RequestMethod.POST)
	public String movieWrite(MovieVO mvo, AwardVO avo, MultipartFile posterFile){
		adminService.movieWrite(mvo, avo, posterFile);
		return "redirect:movie_content.do?movieNo="+mvo.getMovieNo();
	}
	@RequestMapping("/movie_content.do")
	public ModelAndView movieContent(int movieNo){
		return new ModelAndView("movie_content.admin", "mContentMap", adminService.movieContent(movieNo));
	}
	@RequestMapping("/cinemaList.do")
	public ModelAndView cinemaList(String pageNo){
		return new ModelAndView("cinemaList.admin", "clistVO", adminService.cinemaList(pageNo));
	}
	@RequestMapping("/cinema_content.do")
	public ModelAndView cinemaContent(int cinemaNo){
		return new ModelAndView("cinema_content.admin", "cinamaVO", adminService.cinemaContent(cinemaNo));
	}
	@RequestMapping(value="/cinema_write.do", method=RequestMethod.POST)
	public String cinemaWrite(CinemaVO cvo){
		adminService.cinemaWrite(cvo);
		return "redirect:cinema_content.do?cinemaNo="+cvo.getCinemaNo();
	}
	@RequestMapping("/cinema_delete.do")
	public String cinemaDelete(int cinemaNo){
		adminService.cinemaDelete(cinemaNo);
		return "redirect:cinemaList.do";
	}
	@RequestMapping("/cinema_updateView.do")
	public ModelAndView cinemaUpdateView(int cinemaNo){
		return new ModelAndView("cinema_update.admin", "cinameVO", adminService.cinemaUpdateView(cinemaNo));
	}
	@RequestMapping(value="/cinema_update.do", method=RequestMethod.POST)
	public String cinemaUpdate(CinemaVO cvo){
		adminService.cinemaUpdate(cvo);
		return "redirect:cinema_content.do?cinemaNo="+cvo.getCinemaNo();
	}
	@RequestMapping("/movie_updateView.do")
	public ModelAndView movieUpdateView(int movieNo){
		return new ModelAndView("movie_update.admin","updateMap", adminService.movieUpdateView(movieNo));
	}
	@RequestMapping(value="/updateMovie.do", method=RequestMethod.POST)
	public String updateMovie(MovieVO mvo, MultipartFile posterFile){
		adminService.updateMovie(mvo, posterFile);
		return "redirect:movie_content.do?movieNo="+mvo.getMovieNo();
	}
	@RequestMapping("/movie_delete.do")
	public String deleteMovie(int movieNo){
		adminService.deleteMovie(movieNo);
		return "redirect:movieList.do";
	}
	@RequestMapping(value="/showingWrite.do", method=RequestMethod.POST)
	public String showingWrite(ShowingVO svo){
		adminService.showingWrite(svo);
		return "redirect:showingmovie_content.do?showingNo="+svo.getMovieVO().getMovieNo();
	}
	@RequestMapping("/memberList.do")
	public ModelAndView memberList(String pageNo){
		return new ModelAndView("memberList.admin", "memberListVO", adminService.memberList(pageNo));
	}
	@RequestMapping("/checkCinema.do")
	@ResponseBody
	public boolean checkCinema(int cinemaNo, int movieNo){
		return adminService.checkCinema(cinemaNo, movieNo);
	}
	@RequestMapping("/showing_delete.do")
	public String showingDelete(int showingNo, int movieNo){
		String path = "";
		adminService.deleteShowing(showingNo);		
		int result = adminService.confirmShowingContent(movieNo);
		if(result==0){
			path = "redirect:showingMovieList.do";
		}else{
			path = "redirect:showingmovie_content.do?showingNo="+movieNo;
		}		
		return path;
	}
	@RequestMapping("/showingMovieList.do")
	public ModelAndView showingMovieList(String pageNo){
		return new ModelAndView("showingmovielist.admin","sListVo",adminService.getShowingListByShowingNo(pageNo));
	}	
	@RequestMapping("/showingmovie_writeView.do")
	public ModelAndView showingMovieWrite(){
		return new ModelAndView("showingmovie_write_view.admin","allInfoMap",adminService.findAllMovieInfo());
	}
	@RequestMapping(value="/allShowingWrite.do", method=RequestMethod.POST)
	public String allShowingWrite(ShowingVO svo){
		adminService.showingWrite(svo);
		return "redirect:showingmovie_content.do?showingNo="+svo.getMovieVO().getMovieNo();
	}
	@RequestMapping("/showingmovie_content.do")
	public ModelAndView showingMovieContent(int showingNo){
		return new ModelAndView("showingmovie_content.admin", "allInfoMap", adminService.getShowingListByShowingNo(showingNo));
	}
	@RequestMapping("/schedule.do")
	public ModelAndView scheduleWriteView(int showingNo){		
		return new ModelAndView("schedule.admin","allInfoMap",adminService.getSchedule(showingNo));
	}
	@RequestMapping("/getSchedule.do")
	@ResponseBody
	public List<String> getScheduleByShowingNo(int showingNo){
		return adminService.getScheduleByShowingNo(showingNo);
	}
	@RequestMapping("/updateSchedule.do")
	@ResponseBody
	public List<String> updateSchedule(ScheduleDateVO sdvo){
		adminService.updateSchedule(sdvo);
		return adminService.getScheduleByShowingNo(sdvo.getShowingVO().getShowingNo());
	}
	@RequestMapping("/deleteSchedule.do")
	public ModelAndView deleteSchedule(ShowingVO svo){
		adminService.deleteSchedule(svo);
		return new ModelAndView("schedule.admin","allInfoMap",adminService.getSchedule(svo.getShowingNo()));
	}
}
